import BaseEvent from '@/components/events/BaseEvent'
import { getCompIdByParam, isJSON, isHasOwnProperty } from '@/plugins/utils'
import { CONTROL_TYPES } from '@/common/const-wb'
import { EventBus } from '@/store/index'

export default class InteractionDataEvent extends BaseEvent{
  constructor() {
    super();
  }

  excute({item, event}){
    this.item = item;
    this.event = event;
    let rotateUrl;
    let randomIndex = -1;
    let lists = item.interactionData.lists;
    let customStr = event.comps[0].attrs.value;
    let relateCompId = getCompIdByParam(event.comps[1].attrs.value);
    let relateCompData = this.componentMap[relateCompId];
    if(relateCompId && relateCompData && relateCompData.commonAttr.itemIndex >= 0 && lists){
      randomIndex = relateCompData.commonAttr.itemIndex;
      this.item.commonAttr.itemIndex = randomIndex;

      if(lists[randomIndex]){
        rotateUrl = lists[randomIndex].url;
      }
      if (rotateUrl) {
        let shareData = { url: rotateUrl }
        this.store.commit('updateShareData', shareData);
      }
    }else if(customStr && isJSON(customStr)){
      // {"signDay":1,"btnCompId":"05c6a3f2-3f7d-40c6-b286-2512c6aa98e0"} 1911
      let customObj = JSON.parse(customStr);
      if(isHasOwnProperty(customObj, "btnCompId")){
        let btnCompId = customObj.btnCompId;
        let count = 0;
        let commonAttr = {filter: { isGrayscale: 0, grayscale: 5 }};
        if(this.getItemControls[CONTROL_TYPES.vote]){
          let elements = this.getItemControls[CONTROL_TYPES.vote].data.elements;
          _.forEach(elements, (vote) => {
            if(vote.num > 0){
              count++;
            }
          })
        } else if(this.getItemControls[CONTROL_TYPES.number]) {
          count = this.getItemControls[CONTROL_TYPES.number].data.num;
        }
        if(count < customObj.signDay){
          commonAttr.filter.isGrayscale = true;
        }else {
          commonAttr.filter.isGrayscale = false;
        }
        this.store.commit("updateComponentAttr", {
          id: btnCompId,
          commonAttr,
        });
        EventBus.$emit('refreshImgStyles', { id: btnCompId });
      }else if(isHasOwnProperty(customObj, "lottery")){
        // 2113 自定义数据-点击 {"lottery": {"3": "b5c5a0ba-b3ad-4088-a9cd-dd82ac42a769","7": "080a918f-9ab6-4beb-9c47-b95d17802446","14": "88236e33-b128-48d4-9577-8159c38ca331"}}
        let compId = item.events.interactionData.comps[1] && getCompIdByParam(item.events.interactionData.comps[1].attrs.value)
        if(compId && compId.length){
          let num = this.formValues[compId].value;
          let max = 0
          _.forEach(customObj.lottery,(value, key) =>{
            key = Number(key)
            if(num >= Number(key)){
              if(max < key){
                max = key
              }
            }
          })
          let maxCompId = customObj.lottery[max]
          if(!maxCompId){
            this.showToast('亲，签到次数不够！')
            return
          }
          EventBus.$emit("itemClick", {id: maxCompId})
        }
      }else if(isHasOwnProperty(customObj, "isEmitClick")){
        EventBus.$emit('radioImg',{index: this.item.commonAttr.itemIndex})
      }
    }
  }

  destroy(){
    super.destroy();
  }
}
